#ifndef __FLASH_H__
#define __FLASH_H__
#include "gd32f10x.h"

#define PAGE_SIZE	        1024	    //页大小
#define PAGE_NUM	        128		    //页数
/* 分区         大小        扇区        说明
bootloader      9K         0-8       引导程序
flag            1K         9         版本信息
app             59K        10-68     应用程序
download		59K		   69-127	 下载区
*/
#define BOOTLOADER_PAGE_NUM     (9)
#define VERSION_INFO_PAGE_NUM   (1)
#define APPLICATION_PAGE_NUM    (59)
#define DOWNLOAD_PAGE_NUM      	(59)

/* bootloader分区 */
#define BOOTLOADER_ADDR         (0x08000000U)
#define BOOTLOADER_PAGE_START   (0)
#define BOOTLOADER_FLASH_SIZE   (PAGE_SIZE * BOOTLOADER_PAGE_NUM)
/* 版本信息分区 */
#define VERSION_INFO_ADDR       (BOOTLOADER_ADDR + BOOTLOADER_FLASH_SIZE)
#define VERSION_INFO_PAGE_START (BOOTLOADER_PAGE_START + BOOTLOADER_PAGE_NUM)
#define VERSION_INFO_FLASH_SIZE (PAGE_SIZE * VERSION_INFO_PAGE_NUM)
/* 应用程序分区 */
#define APPLICATION_ADDR        (VERSION_INFO_ADDR + VERSION_INFO_FLASH_SIZE)
#define APPLICATION_PAGE_START  (VERSION_INFO_PAGE_START + VERSION_INFO_PAGE_NUM)
#define APPLICATION_FLASH_SIZE  (PAGE_SIZE * APPLICATION_PAGE_NUM)
/* 下载分区 */
#define DOWNLOAD_ADDR          	(APPLICATION_ADDR + APPLICATION_FLASH_SIZE)
#define DOWNLOAD_PAGE_START    	(APPLICATION_PAGE_START + APPLICATION_PAGE_NUM)
#define DOWNLOAD_FLASH_SIZE    	(PAGE_SIZE * DOWNLOAD_PAGE_NUM)

/* 版本信息 */
#define VERSION_INFO_UPDATE_ADDR	(VERSION_INFO_ADDR)
#define VERSION_INFO_FILESIZE_ADDR	(VERSION_INFO_ADDR + 4)	

/* 升级密钥 */
#define UPDATE_KEY              (0x6D78346B)
void fmc_erase_page(uint32_t pageAddr);
void fmc_program(uint32_t pageAddr, uint8_t *data, uint16_t len);
#endif /* __FLASH_H__ */
